package demo.dao;

import demo.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Auther: huagui
 * @Date: 2021/3/25 - 18:19
 * @Description: demo.dao
 */
public class StudentTest01 {
    /**
     * 查询全部
     */
    @Test
    public void selectAll() throws IOException {
        //加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        //获取SqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        //通过工厂对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //执行映射配置文件的sql语句，并接收结果
        List<Student> list = sqlSession.selectList("StudentMapper.selectAll");

        //处理结果
        for (Student student:
             list) {
            System.out.println(student);
        }

        //释放资源
        sqlSession.close();
        is.close();
    }
    /**
     * 根据id查询
     *
     */
    @Test
public  void selectById() throws IOException {
    //1.加载核心配置文件
    InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
    //2.获取sqlSession工厂对象
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    //3.通过工厂对象获取sqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //4.执行映射配置文件的sql语句
        Student stu = sqlSession.selectOne("StudentMapper.selectById", 1);
        //5.处理结果
        System.out.println(stu);

    //6.释放资源
        sqlSession.close();
        is.close();
}
/**
 *新增
 */
    @Test
    public void insert() throws IOException {
        //1.加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        //2.获取sqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //3.获取sqlSession对象
        //sqlSession默认false，则需要手动提交事务sqlSession.commit();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //4.执行映射文件中的sql语句
        Student student = new Student(6,"二流子" , 25);
        int i = sqlSession.insert("StudentMapper.insert", student);

        //5.处理结果
        if (i!=0){
            System.out.println("新增成功");
        }else {
            System.out.println("新增失败");
        }
        //6.释放资源
        sqlSession.close();
        is.close();
    }
    /**
     *
     * 修改
     */
    @Test
    public void update() throws IOException {
        //1.加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        //2.获取sqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //3.通过工厂对象获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.执行映射配置文件的sql语句
        Student s = new Student(4, "刘齐全", 20);
        int update = sqlSession.update("StudentMapper.update", s);
        sqlSession.commit();
        //5.处理结果
        System.out.println(update);

        //6.释放资源
        sqlSession.close();
        is.close();
    }
    /**
     *
     * 通过id删除
     */
    @Test
    public void delete() throws Exception {
        //1.加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        //2.获取sqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //3.通过工厂对象获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.执行映射配置文件的sql语句
        int delete = sqlSession.delete("StudentMapper.delete",5);
        sqlSession.commit();
        //5.处理结果
        System.out.println(delete);

        //6.释放资源
        sqlSession.close();
        is.close();
    }
}
